package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkDispatchEngineerCooperate;
import com.zb.mapper.ZjWorkDispatchEngineerCooperateMapper;
import com.zb.service.ZjWorkDispatchEngineerCooperateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单分派工程师协同表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-23
 */
@Service
public class ZjWorkDispatchEngineerCooperateServiceImpl extends ServiceImpl<ZjWorkDispatchEngineerCooperateMapper, ZjWorkDispatchEngineerCooperate> implements ZjWorkDispatchEngineerCooperateService {
    @Resource
    private ZjWorkDispatchEngineerCooperateMapper workDispatchEngineerCooperateMapper;

    // 保存协同工程师
    @Override
    public void save(Long custAccId, Long workId, Long workDispatchEngineerId, List<Long> engineerCooperateIds) {
        if (CollectionUtil.isEmpty(engineerCooperateIds)) {
            return;
        }
        workDispatchEngineerCooperateMapper.deleteByWorkDispatchEngineerId(workDispatchEngineerId);
        List<ZjWorkDispatchEngineerCooperate> cooperateList = new ArrayList<>();
        ZjWorkDispatchEngineerCooperate engineerCooperate = null;
        for (Long engineerCooperateId : engineerCooperateIds) {
            engineerCooperate = new ZjWorkDispatchEngineerCooperate();
            engineerCooperate.setId(IdWorker.getId());
            engineerCooperate.setCustAccId(custAccId);
            engineerCooperate.setWorkId(workId);
            engineerCooperate.setWorkDispatchEngineerId(workDispatchEngineerId);
            engineerCooperate.setNetworkEmpId(engineerCooperateId);
            cooperateList.add(engineerCooperate);
        }
        this.saveBatch(cooperateList);
    }

    // 根据工单分派工程师表ID获取云协同工程师
    @Override
    public List<ZjWorkDispatchEngineerCooperate> getByWorkDispatchEngineerId(Long workDispatchEngineerId) {
        QueryWrapper<ZjWorkDispatchEngineerCooperate> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_dispatch_engineer_id", "network_emp_id");
        wrapper.eq("work_dispatch_engineer_id", workDispatchEngineerId);
        return this.list(wrapper);
    }

    // 根据服务人员ID获取协同工单数量
    @Override
    public long getCooperateOrderQty(Long custAccId, Long netwokId, Long networkEmpId) {
        return workDispatchEngineerCooperateMapper.getCooperateOrderQty(custAccId, netwokId, networkEmpId);
    }
}
